package com.gitee.huanminabc.utils_tools.file_tool.excel.easy;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.NumberUtils;

import java.text.ParseException;

/**
 * @description: 兼容Long转换String , 内置的不会兼容数字等类型转化为Long
 * @author: huanmin
 * @create: 2025-01-02 17:28
 **/
public class EasyLongStringConverter implements Converter<Long> {
    @Override
    public Long convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
                                  GlobalConfiguration globalConfiguration) throws ParseException {
        //判断
        if (cellData.getStringValue()!=null){
            return NumberUtils.parseLong(cellData.getStringValue(), contentProperty);
        }
       //判断数值
        if (cellData.getNumberValue()!=null){
            return cellData.getNumberValue().longValue();
        }
        return null;
    }

    @Override
    public WriteCellData<?> convertToExcelData(Long value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        return NumberUtils.formatToCellDataString(value, contentProperty);
    }
}
